﻿function Infra_ActionPermission() {
	// 功能权限Action名称选择ComboStore
	var ActionPermissionActionNameStore = new Ext.data.JsonStore({
				url : '/InfraActionPermission/QueryActionPlist',
				root : 'data',
				totalProperty : 'total',
				id : 'Id',
				fields : ['ActionName', 'Id', 'ControllerName', 'Description']
			});

	// 功能权限表格Store
	var ActionPermissionitemGridStore = new Ext.data.Store({
		url : "/InfraActionPermission/GetPlistActionPermission",
		remoteSort : true,
		reader : new Ext.data.JsonReader({
					root : 'data',
					totalProperty : 'total',
					id : 'Id'
				}, [{
							name : 'Id',
							type : 'string'
						}, {
							name : 'ControllerName',
							type : 'string'
						}, {
							name : 'ActionName',
							type : 'string'
						}, {
							name : 'Description',
							type : 'string'
						}, {
							name : 'CreateTime',
							type : 'date'
						}]),
		listeners : {
			'update' : function(thiz, record, operation) {
				var model = record.data;
				if (operation == Ext.data.Record.EDIT
						&& !record.getChanges().Id) {
					Ext.Ajax.request({
						url : '/InfraActionPermission/SaveOrUpdateActionPermission',
						params : model,
						success : function(response, opts) {
							var result = Ext.util.JSON
									.decode(response.responseText);
							if (result.success) {
								if (!record.get('Id'))
									record.set('Id', result.id);
								thiz.commitChanges();
							} else {
								JsHelper.ShowError(result.msg);
								thiz.rejectChanges();
							}
						}
					});
				}
			},
			'remove' : function(thiz, record, index) {
				Ext.Ajax.request({
							url : '/InfraActionPermission/DeleteActionPermission',
							params : {
								'Id' : record.data.Id
							},
							success : function(response, opts) {
								var result = Ext.util.JSON
										.decode(response.responseText);
								if (result.success == false) {
									JsHelper.ShowError(result.msg);
									ActionPermissionitemGridStore.insert(0,
											record);
								}
							}
						});
			}
		}
	});
	ActionPermissionitemGridStore.setDefaultSort('ControllerName', 'ASC');

	// 功能权限Editor
	var ActionPermissionActionNameComboTextTmp = null;
	var ActionPermissionControllerNameComboTextTmp = null;
	var ActionPermissionDescriptionComboTextTmp = null;
	var ActionPermissiongridEditor = new Ext.ux.grid.RowEditor({
		listeners : {
			'beforeedit' : function() {
				ActionPermissionActionNameComboTextTmp = null;
				ActionPermissionControllerNameComboTextTmp = null;
				ActionPermissionDescriptionComboTextTmp = null;
			},
			'beforerecordsubmit' : function(r, cm, ri) {
				if (ActionPermissionActionNameComboTextTmp == null) {
					r.set('ActionName', cm.getCellEditor(2, ri).getValue());
				} else {
					r.set('ActionName', ActionPermissionActionNameComboTextTmp);
				}
				if (ActionPermissionDescriptionComboTextTmp == null) {
					r.set('Description', cm.getCellEditor(4, ri).getValue());
				} else {
					r.set('Description',
							ActionPermissionDescriptionComboTextTmp);
				}
				if (ActionPermissionControllerNameComboTextTmp == null) {
					r.set('ControllerName', r.get('ControllerName'));
				} else {
					r.set('ControllerName',
							ActionPermissionControllerNameComboTextTmp);
				}
			}
		}
	});

	// 功能权限filters
	var filters = new Ext.ux.grid.GridFilters({
				encode : true,
				filters : [{
							type : 'string',
							dataIndex : 'ControllerName'
						}, {
							type : 'string',
							dataIndex : 'ActionName'
						}, {
							type : 'string',
							dataIndex : 'Description'
						}, {
							type : 'date',
							dataIndex : 'CreateTime'
						}]
			});

	// 功能权限表格列
	var ActionPermissionsm = new Ext.grid.CheckboxSelectionModel();
	var ActionPermissioncm = new Ext.grid.ColumnModel([
			new Ext.grid.RowNumberer(), ActionPermissionsm, {
				header : "Action名称",
				dataIndex : 'ActionName',
				sortable : true,
				width : 300,
				editor : {
					xtype : 'sicombo',
					displayField : 'ActionName',
					store : ActionPermissionActionNameStore,
					emptyText : "请选择...",
					allowBlank : false,
					listWidth : 920,
					tpl : new Ext.XTemplate(
							'<div class="x-grid3"><div><table>',
							'<thead class="x-grid3-header"><tr class="x-grid3-hd-row">',
							'<td class="x-grid3-hd x-grid3-cell x-grid3-hd-inner" style="width: 300px;color: blue;">Action名称</td>',
							'<td class="x-grid3-hd x-grid3-cell" style="width: 300px;color: blue;">Controller名称</td>',
							'<td class="x-grid3-hd x-grid3-cell" style="width: 300px;color: blue;">描述</td>',
							'</tr></thead><tbody>',
							'<tpl for=".">',
							'<tr class="x-combo-list-item">',
							'<td class="x-grid3-col x-grid3-cell"><div class="x-grid3-cell-inner"  style="font-size:12px;">{ActionName}</div></td>',
							'<td class="x-grid3-col x-grid3-cell"><div class="x-grid3-cell-inner"  style="font-size:12px;">{ControllerName}</div></td>',
							'<td class="x-grid3-col x-grid3-cell"><div class="x-grid3-cell-inner"  style="font-size:12px;">{Description}</div></td>',
							'</tr>', '</tpl>', '</tbody></table></div></div>'),
					listeners : {
						'beforesetvalue' : function(r) {
							ActionPermissionActionNameComboTextTmp = r
									.get('ActionName');// Combox的处理从这里看起...先把文本值给放在一个临时值里,鼠标滚轮翻到最上头见此值定义
							ActionPermissionControllerNameComboTextTmp = r
									.get('ControllerName');
							ActionPermissionDescriptionComboTextTmp = r
									.get('Description');
						}
					}
				},
				updateEdit : false
			}, {
				header : "Controller名称",
				dataIndex : 'ControllerName',
				sortable : true,
				width : 300
			}, {
				header : "描述",
				dataIndex : 'Description',
				sortable : true,
				width : 300,
				editor : {
					xtype : 'textfield'
				}
			}, {
				xtype : 'datecolumn',
				format : 'Y年m月d日 H:i:s',
				header : "创建时间",
				sortable : true,
				dataIndex : 'CreateTime',
				width : 150
			}]);

	// 功能权限表格工具栏
			var ActionPermissiontbar = [{
			    xtype: "tbbutton",
			    minWidth: 80,
			    ref: '../addBtn',
			    text: "添加项",
			    disabled: false,
			    cls: "x-btn-text-icon",
			    icon: "/Content/icons/add.png",
			    handler: function (btn, e) {
			        ActionPermissiongridEditor.stopEditing();
			        ActionPermissionitemGridStore.insert(0,
								new ActionPermissionitemGridStore.recordType);
			        ActionPermissiongrid.getView().refresh();
			        ActionPermissiongrid.getSelectionModel().selectRow(0);
			        ActionPermissiongridEditor.startEditing(0);
			    }
			}, "-", {
			    xtype: "tbbutton",
			    ref: '../delBtn',
			    minWidth: 80,
			    disabled: true,
			    text: "删除项",
			    cls: "x-btn-text-icon",
			    icon: "/Content/icons/delete.png",
			    handler: function (btn, e) {
			        JsHelper.DelConfirm(function (btn) {
			            if (btn == 'yes') {
			                ActionPermissiongridEditor
												.stopEditing();
			                var s = ActionPermissiongrid
												.getSelectionModel()
												.getSelections();
			                for (var i = 0, r; r = s[i]; i++) {
			                    ActionPermissionitemGridStore
													.remove(r);
			                }
			            }
			        });
			    }
			}, "-", {
			    xtype: "tbbutton",
			    minWidth: 80,
			    text: "双击修改项",
			    cls: "x-btn-text-icon",
			    icon: "/Content/icons/pencil.png",
			    disabled: true
			}, "-", {
			    xtype: "tbbutton",
			    minWidth: 80,
			    text: "重置功能权限系统缓存",
			    cls: "x-btn-text-icon",
			    icon: "/Content/icons/exclamation.png",
			    handler: function (btn, e) {
			        var mask = new Ext.LoadMask(Ext.getBody(), {
			            msg: '缓存重置中...',
			            removeMask: true
			        });
			        mask.show();
			        Ext.Ajax.request({
			            url: '/InfraActionPermission/ResetCache',
			            success: function (response, opts) {
			                mask.hide();
			                var result = Ext.util.JSON
												.decode(response.responseText);
			                if (result.success == false) {
			                    JsHelper.ShowError(result.msg);
			                }
			            }
			        });
			    }
			}];

	// 功能权限分页工具栏
	var ActionPermissionbbar = new Ext.PagingToolbar({
				displayInfo : true,
				emptyMsg : "没有数据显示",
				displayMsg : "{0}到{1}条，共{2}条",
				store : ActionPermissionitemGridStore,
				pageSize : ALL_PAGESIZE_SETTING,
				plugins : [filters]
			})

	// 功能权限维护表格
	var ActionPermissiongrid = new Ext.grid.GridPanel({
				region : "center",
				enableColumnMove : false,
				sm : ActionPermissionsm,
				cm : ActionPermissioncm,
				plugins : [ActionPermissiongridEditor, filters],
				stripeRows : true,
				frame : true,
				border : true,
				layout : "fit",
				store : ActionPermissionitemGridStore,
				loadMask : true,
				tbar : ActionPermissiontbar,
				bbar : ActionPermissionbbar
			});

	// 控制删除按钮
	ActionPermissiongrid.getSelectionModel().on('selectionchange',
			function(ActionPermissionsm) {
				ActionPermissiongrid.delBtn.setDisabled(ActionPermissionsm
						.getCount() < 1);
			});

	// 加载
	JsHelper.ExtTabDoLayout(ActionPermissiongrid);

	ActionPermissionitemGridStore.load({
				params : {
					start : 0,
					limit : ALL_PAGESIZE_SETTING
				}
			});
}

Infra_ActionPermission();